package de.dopichaj.labrador.tools;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.Map.Entry;

import de.dopichaj.labrador.index.Index;
import de.dopichaj.labrador.index.IndexVersionException;
import de.dopichaj.labrador.index.TagFactory;
import de.dopichaj.labrador.index.meta.DocumentMetaData;
import de.dopichaj.labrador.index.meta.HitFactory;
import de.dopichaj.labrador.index.own.OwnFileSystemIndex;
import de.dopichaj.labrador.search.hit.Hit;
import de.dopichaj.labrador.search.hit.HitHierarchyTree;
import de.dopichaj.labrador.util.CountingMap;
import de.dopichaj.labrador.util.MeanCounter;


public final class CollectionStats {

    private static final class ElementCount {
        public int elementCount = 0;
        public int leafElementCount = 0;
    }
    
    private static final class LevelStats {
        private final MeanCounter lengthCounter = new MeanCounter();
        private final MeanCounter fanoutCounter = new MeanCounter();
        private final CountingMap<String> elementCount = new CountingMap<String>();
        private final String exampleXPath;
        
        public LevelStats(final String exampleXPath) {
            this.exampleXPath = exampleXPath;
        }
        
        public MeanCounter getFanoutCounter() {
            return fanoutCounter;
        }
        public MeanCounter getLengthCounter() {
            return lengthCounter;
        }
        
        public CountingMap<String> getElementCount() {
            return elementCount;
        }
        
        public String getElementString() {
            final StringBuilder builder = new StringBuilder();
            String prefix = "";
            for (final String name : elementCount.keySet()) {
                builder.append(prefix).append(name).append("[").append(elementCount.get(name)).append("]");
                prefix = ",";
            }
            return builder.toString();
        }
        
        public String getExampleXPath() {
            return exampleXPath;
        }
    }
    
    private static final class Collector implements HitFactory.DigestCollector {

        private int documentCount = 0;

        /**
         * Counts the elements per document.
         */
        private final MeanCounter elementCounter = new MeanCounter();
        /**
         * The global use of element names in the collection.
         */
        private final CountingMap<String> elementNameCounter =
            new CountingMap<String>();
        private final MeanCounter leafElementCounter = new MeanCounter();
        
        private final MeanCounter depthCounter = new MeanCounter();
        /**
         * A list of level-specific statistics.
         */
        private final List<LevelStats> levelStats = new ArrayList<LevelStats>();
        
        public void collectDigest(final DocumentMetaData digest) {
        
            documentCount++;
            
            final Collection<Hit> hits = digest.getFragments().values();
            final HitHierarchyTree tree = new HitHierarchyTree(hits);
            
            addTree(tree);
        }

        private void addTree(final HitHierarchyTree tree) {

            final ElementCount elementCount = new ElementCount();
            depthCounter.add(traverse(tree, tree.getRoot(), 0, elementCount));
            elementCounter.add(elementCount.elementCount);
            leafElementCounter.add(elementCount.leafElementCount);
        }
        
        private LevelStats getLevelStats(final int level, final Hit hit) {
            if (levelStats.size() <= level) {
                levelStats.add(level, new LevelStats(hit.getXPath()));
            }
            final LevelStats stats = levelStats.get(level);
            stats.getElementCount().count(hit.getMarkup());
            stats.getLengthCounter().add(hit.getContentLength());
            elementNameCounter.count(hit.getMarkup());
            return stats;
        }
        
        /**
         * Traverse the substree below <code>hit</code> and record statistics
         * of fanout.
         * The given hit is assumed to be at the given level.

         * @return the depth of the subtree of <code>hit</code>,
         *      including <code>hit</code>itself.
         */
        private int traverse(final HitHierarchyTree tree, final Hit hit,
            final int level, final ElementCount elementCount) {
            
            elementCount.elementCount++;
            
            LevelStats stats = getLevelStats(level, hit);
            
            // get the non-empty children
            final SortedSet<Hit> children = tree.getChildren(hit);
            final List<Hit> nonEmptyChildren = new ArrayList<Hit>(children.size());
            for (final Hit child : children) {
                if (child.getContentLength() != 0) {
                    nonEmptyChildren.add(child);
                }
            }
            
            // update the fanout counter
            final MeanCounter fanoutCounter = stats.getFanoutCounter();
            fanoutCounter.add(nonEmptyChildren.size());
            
            // recurse the non-empty children
            int depth = 1;
            for (final Hit child : nonEmptyChildren) {
                final int subtreeDepth =
                    traverse(tree, child, level + 1, elementCount);
                if (subtreeDepth + 1 > depth) {
                    depth = subtreeDepth + 1;
                }
            }
            
            // update the leaf element counter
            if (nonEmptyChildren.isEmpty()) {
                elementCount.leafElementCount++;
            }
            
            return depth;
        }
        
        public List<LevelStats> getLevelStats() {
            return levelStats;
        }
        
        public int getDocumentCount() {
            return documentCount;
        }
        
        public MeanCounter getElementCounter() {
            return elementCounter;
        }
        
        public MeanCounter getLeafElementCounter() {
            return leafElementCounter;
        }
        
        public MeanCounter getDepthCounter() {
            return depthCounter;
        }
        
        public Map<String, Integer> getElementNameCounts() {
            return elementNameCounter;
        }
    }

    public static void main(String[] args) throws IOException, IndexVersionException {
        
        // open the de.dopichaj.labrador.index
        final Index index = new OwnFileSystemIndex(new File(args[0]));
        final TagFactory tagFactory = index.getTagFactory();
        final Collector collector = new Collector();
        
        HitFactory.readDigests(index.getDocumentMetaDataFactory(), tagFactory,
            index.getDigestByteReader(), collector);
        
        final int levelMax = collector.getDepthCounter().getMax();
        final Iterator<LevelStats> iterator = collector.getLevelStats().iterator();
        LevelStats stats;
        for (int level = 0; level < levelMax; level++) {
            stats = iterator.next();
            final MeanCounter fanoutCounter = stats.getFanoutCounter();
            final MeanCounter lengthCounter = stats.getLengthCounter();
            
            System.out.println("Level " + level + " FANOUT " + fanoutCounter +
                " LENGTH " + lengthCounter + " COUNT " + fanoutCounter.getCount() +
                "  mean-below " +
                ((double)fanoutCounter.getSum() / collector.getDocumentCount()) +
                " elements " + stats.getElementString() +
                " example " + stats.getExampleXPath());
        }
        
        System.err.println("Elements per document: " +
            collector.getElementCounter());
        System.err.println("Leaf elements per document: " +
            collector.getLeafElementCounter());
        System.err.println("Depth: " + collector.getDepthCounter());
        for (final Entry<String, Integer> entry : collector.getElementNameCounts().entrySet()) {
            System.err.println(entry.getKey() + "\t" + entry.getValue());
        }
    }

}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/